Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the possibility to use scopes with arguments, by passing a array or a lambda via the scopes option and more. #121

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

budu
Copy link

@budu budu commented Dec 5, 2011

This can be really useful in certain situations, there's two ways of using scopes with arguments, by sending an array and with a lambda. Here's an example with the former:

autocomplete :user, :name, :scopes => [[:older_than, 21]]

And for lambdas:

before_filter :find_settings
attr_reader :settings
autocomplete :user, :name, :scopes => [lambda {|users, options| 
  users.older_than options[:controller].settings.minimum_age}]

The lambda way isn't really as nice as I'd like, maybe giving the lambda direct access to the controller environment would be better but I'm not sure how to do that.

Update

I've also added the possibility to use a custom action name and improved the way the extra_data option work by making accept hashes, here's a complete example:

  autocomplete(:user,
               :full_name,
               :full => true,
               :action_name => :autocomplete_primes,
               :scopes => [lambda { |items, options|
                   items.primes_by_course options[:controller].course }],
               :extra_data => {'special_pricings.cost' => lambda { |item, options| item.special_pricings.first.cost }})

Edit: Updated last code example to reflect changes in 1f09e1f.

@softr8
Copy link

softr8 commented Dec 7, 2011

Thanks for this pull request, it looks really interesting, just one thing, can you add some test coverage? I ran tests and some of them are broken

@softr8 softr8 closed this Dec 7, 2011
@softr8 softr8 reopened this Dec 7, 2011
@budu
Copy link
Author

budu commented Dec 7, 2011

sorry, made those changes in a rush, I'll have a look at the tests!

@budu
Copy link
Author

budu commented Dec 7, 2011

I've fixed the existing tests and added some new unit tests for the json_for_autocomplete method. What's missing is some unit tests for get_autocomplete_items and the integration tests for the new features in this pull request. I'm not quite sure how to approach the later, but I could add the missing unit tests in the coming week.

…on that generate an id field and set the id_element html option.
@budu
Copy link
Author

budu commented Dec 8, 2011

I've made another change that I think would be worthwhile. For the Formtastic custom input, you can now do:

f.input :user_name, :as => :autocomplete, :id_field => :user_id

and it will generate the id field then set the id_element option.

I understand this pull request is starting to get big, that's because I'm lazy, so if you don't like that last feature I could move it to another branch.

@caarlos0
Copy link

caarlos0 commented Apr 9, 2013

please merge this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants